<?php
require_once 'Zend/Db/Table.php';
require_once 'application/library/db/gcDb.php';
require_once 'application/constant/dbConstant.php';
require_once 'application/library/config/gcConfigRegistry.php';

/** 
 * 检索用类
 * gcDbSelect
 * @author    Soochow University
 * @version   1.0
 * @exclude   作  成  日：2011/04/07
 */
class gcDbSelect{

    private $gcDb;

    /**
     * __construct
     */
    public function __construct(){
        $this->gcDb=new gcDb();
    }

    /**
     * 类型对应检索
     * Select
     * @param   string  $sql
     * @param   string  $parameter
     * @param   string  $type
     * @return  array
     */
    public function Select($sql,$parameter,$type){
        if(empty($parameter)){
            $parameter=array();
        }
        switch ($type){
            case dbReturnType::ALL:
                return $this->fetchAll($sql,$parameter);
                break;
            case dbReturnType::ASSOC:
                return $this->fetchAssoc($sql,$parameter);
                break;
            case dbReturnType::COL:
                return $this->fetchCol($sql,$parameter);
                break;
            case dbReturnType::ONE:
                return $this->fetchOne($sql,$parameter);
                break;
            case dbReturnType::ROW:
                return $this->fetchRow($sql,$parameter);
                break;
            default:
                return $this->query($sql,$parameter);
        }
    }

    /**
     * 全部返回
     * fetchAll
     * @param   string  $sql
     * @param   string  $parameter
     * @return  array
     */
    private function fetchAll($sql,$parameter){
        $this->gcDb->openDb();
        $result = $this->gcDb->db->fetchAll($sql, $parameter);
        $this->gcDb->db->closeConnection();
        return $result;
    }

    /**
     * 数字索引返回
     * fetchAssoc
     * @param   string  $sql
     * @param   string  $parameter
     * @return  array
     */
    private function fetchAssoc($sql,$parameter){
        $this->gcDb->openDb();
        $result = $this->gcDb->db->fetchAssoc($sql, $parameter);
        $this->gcDb->db->closeConnection();
        return $result;
    }

    /**
     * 列返回
     * fetchCol
     * @param   string  $sql
     * @param   string  $parameter
     * @return  array
     */
    private function fetchCol($sql,$parameter){
        $this->gcDb->openDb();
        $result = $this->gcDb->db->fetchCol($sql, $parameter);
        $this->gcDb->db->closeConnection();
        return $result;
    }

    /**
     * 单个值返回
     * fetchOne
     * @param   string  $sql
     * @param   string  $parameter
     * @return  array
     */
    private function fetchOne($sql,$parameter){
        $this->gcDb->openDb();
        $result = $this->gcDb->db->fetchOne($sql, $parameter);
        $this->gcDb->db->closeConnection();
        return $result;
    }

    /**
     * 行返回
     * fetchRow
     * @param   string  $sql
     * @param   string  $parameter
     * @return  array
     */
    private function fetchRow($sql,$parameter){
        $this->gcDb->openDb();
        $result = $this->gcDb->db->fetchRow($sql, $parameter);
        $this->gcDb->db->closeConnection();
        return $result;
    }

    /**
     * SQL语句执行
     * query
     * @param   string  $sql
     * @param   string  $parameter
     * @return  array
     */
    private function query($sql,$parameter){
        $this->gcDb->openDb();
        $result = $this->gcDb->db->query($sql, $parameter);
        $this->gcDb->db->closeConnection();
        return $result;
    }
}